#include <stdio>
#include <stdlib>
#include <sqlite3.h>
#include <string>

class Fichier
{
	private:
		String m_author;
		String m_name;
		String m_desc;
		long m_size;
	
	public:
		
		Fichier(String author, String name, String desc, String size)
		{
			m_author = author;
			m_name = name;
			m_desc = desc;
			m_size = stol(size);
		}
		
		String getAuthor()
		{
			return m_author;
		}
		
		String getName()
		{
			return m_name;
		}
		
		String getDesc()
		{
			return m_desc;
		}
		
		long getSize()
		{
			return m_size;
		}
	
}

class Search
{

	private:
		
		sqlite3 *m_db;
		vector<Fichier>m_results;
		
		int addRes(void *toto, int nb_column, String[] column, String[] columnNames)
		{
			Fichier fichier(column[0], column[1], column[2], column[3]);
			
			m_results.push_back(fichier);
			
			return 0;
		}
		
	public:
		
		Search(sqlite3 *db)
		{
			m_db = db;
		}
		
		vector<Fichier>getRes()
		{
			return m_results;
		}
		
		void operator()(String keyword)
		{
			m_results.clear();
			sqlite3_exec(m_db, "select * from files where name like " + keyword , addRes, NULL, NULL);
		}
}

class Connexion
{
	private:
	
	sqlite3 *m_db;
	
	int checkId(void *success, int nb_column, String[] column, String[] columnNames)
	{
		if(nb_column == 1) *(bool*)success = true;
		return 0;
	}

	
	public:
	
	bool login(String username, String password)
	{
		bool success = false;
		sqlite3_exec(m_db, "select * from identification where username = " + username + " and password = " + password , checkId, (void*)&success, NULL);
		
		if(success)
		{
			sqlite3_exec(m_db, "insert into connexion("+username+", "+ip+")" , NULL, NULL, NULL);
			getFichiers();
		}
		
		return success;
	}
	
}


int main(int argc, char *argv[])
{
	
	delete from t where username = name;
	insert into t(username, name, desc, size);
	
	sqlite3 *db;
	
	if( sqlite3_open("db", &db) )
	{
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		exit(1);
	}
	
	
	
}
